<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageName}.dao.${classInfo.className}Dao">

    <#--返回数据类型必须使用Map,防止数据库数据字段和实体类的字段对应不上导致字段为空-->
    <resultMap id="BaseResultMap" type="${packageName}.bean.${classInfo.className}" >
        <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
            <#list classInfo.fieldList as fieldItem >
                <result column="${fieldItem.columnName}" property="${fieldItem.fieldName}" />
            </#list>
        </#if>
    </resultMap>

    <sql id="Base_Column_List">
        <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
            <#list classInfo.fieldList as fieldItem >
                ${fieldItem.columnName}<#if fieldItem_has_next>,</#if>
            </#list>
        </#if>
    </sql>

    <insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="${packageName}.bean.${classInfo.className}">
        INSERT INTO ${classInfo.tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                <#list classInfo.fieldList as fieldItem >
                    <#if fieldItem.fieldClass == "LocalDateTime" || fieldItem.fieldClass == "Date">
                        <#if fieldItem.columnName != "id" >
                            ${r'<if test ="null != '}${fieldItem.fieldName}${r'">'}
                                ${fieldItem.columnName}<#if fieldItem_has_next>,</#if>
                            ${r"</if>"}
                        </#if>
                    </#if>
                    <#if fieldItem.fieldClass != "LocalDateTime" >
                        <#if fieldItem.columnName != "id" >
                            ${r'<if test ="null != '}${fieldItem.fieldName}${r" AND ''!= "}${fieldItem.fieldName}${r'">'}
                                ${fieldItem.columnName}<#if fieldItem_has_next>,</#if>
                            ${r"</if>"}
                        </#if>
                    </#if>
                </#list>
            </#if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                <#list classInfo.fieldList as fieldItem >
                    <#if fieldItem.fieldClass == "LocalDateTime" || fieldItem.fieldClass == "Date">
                        <#if fieldItem.columnName != "id" >
                            ${r'<if test ="null != '}${fieldItem.fieldName}${r'">'}
                                ${r"#{"}${fieldItem.fieldName}${r"}"}<#if fieldItem_has_next>,</#if>
                            ${r"</if>"}
                        </#if>
                    </#if>
                    <#if fieldItem.fieldClass != "LocalDateTime" >
                        <#if fieldItem.columnName != "id" >
                            ${r'<if test ="null != '}${fieldItem.fieldName}${r" AND ''!= "}${fieldItem.fieldName}${r'">'}
                                ${r"#{"}${fieldItem.fieldName}${r"}"}<#if fieldItem_has_next>,</#if>
                            ${r"</if>"}
                        </#if>
                    </#if>
                </#list>
            </#if>
        </trim>
    </insert>
	
	<insert id="insertSelective" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="${packageName}.bean.${classInfo.className}">
        INSERT INTO ${classInfo.tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                <#list classInfo.fieldList as fieldItem >
                    <#if fieldItem.fieldClass == "LocalDateTime" || fieldItem.fieldClass == "Date">
                        <#if fieldItem.columnName != "id" >
                            ${r'<if test ="null != '}${fieldItem.fieldName}${r'">'}
                                ${fieldItem.columnName}<#if fieldItem_has_next>,</#if>
                            ${r"</if>"}
                        </#if>
                    </#if>
                    <#if fieldItem.fieldClass != "LocalDateTime" >
                        <#if fieldItem.columnName != "id" >
                            ${r'<if test ="null != '}${fieldItem.fieldName}${r" AND ''!= "}${fieldItem.fieldName}${r'">'}
                                ${fieldItem.columnName}<#if fieldItem_has_next>,</#if>
                            ${r"</if>"}
                        </#if>
                    </#if>
                </#list>
            </#if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                <#list classInfo.fieldList as fieldItem >
                    <#if fieldItem.fieldClass == "LocalDateTime" || fieldItem.fieldClass == "Date">
                        <#if fieldItem.columnName != "id" >
                            ${r'<if test ="null != '}${fieldItem.fieldName}${r'">'}
                                ${r"#{"}${fieldItem.fieldName}${r"}"}<#if fieldItem_has_next>,</#if>
                            ${r"</if>"}
                        </#if>
                    </#if>
                    <#if fieldItem.fieldClass != "LocalDateTime" >
                        <#if fieldItem.columnName != "id" >
                            ${r'<if test ="null != '}${fieldItem.fieldName}${r" AND ''!= "}${fieldItem.fieldName}${r'">'}
                                ${r"#{"}${fieldItem.fieldName}${r"}"}<#if fieldItem_has_next>,</#if>
                            ${r"</if>"}
                        </#if>
                    </#if>
                </#list>
            </#if>
        </trim>
    </insert>

    <delete id="deleteByPrimaryKey" >
        deleteByPrimaryKey FROM ${classInfo.tableName}
        WHERE id = ${r"#{id}"}
    </delete>

    <update id="updateByPrimaryKeySelective" parameterType="${packageName}.bean.${classInfo.className}">
        updateByPrimaryKey ${classInfo.tableName}
        <set>
            <#list classInfo.fieldList as fieldItem >
                <#if fieldItem.fieldClass == "LocalDateTime" || fieldItem.fieldClass == "Date">
                    <#if fieldItem.columnName != "id" >
                        ${r'<if test ="null != '}${fieldItem.fieldName}${r'">'}
                            ${fieldItem.columnName} = ${r"#{"}${fieldItem.fieldName}${r"}"},
                        ${r"</if>"}
                    </#if>
                </#if>
                <#if fieldItem.fieldClass != "LocalDateTime" >
                    <#if fieldItem.columnName != "id" >
                        ${r'<if test ="null != '}${fieldItem.fieldName}${r" AND ''!= "}${fieldItem.fieldName}${r'">'}
                        ${fieldItem.columnName} = ${r"#{"}${fieldItem.fieldName}${r"}"},
                        ${r"</if>"}
                    </#if>
                </#if>
            </#list>
        </set>
        WHERE id = ${r"#{"}id${r"}"}
    </update>
	
	<update id="updateByPrimaryKey" parameterType="${packageName}.bean.${classInfo.className}">
        updateByPrimaryKeySelective ${classInfo.tableName}
        <set>
            <#list classInfo.fieldList as fieldItem >
                <#if fieldItem.fieldClass == "LocalDateTime" || fieldItem.fieldClass == "Date">
                    <#if fieldItem.columnName != "id" >
                        ${r'<if test ="null != '}${fieldItem.fieldName}${r'">'}
                            ${fieldItem.columnName} = ${r"#{"}${fieldItem.fieldName}${r"}"},
                        ${r"</if>"}
                    </#if>
                </#if>
                <#if fieldItem.fieldClass != "LocalDateTime" >
                    <#if fieldItem.columnName != "id" >
                        ${r'<if test ="null != '}${fieldItem.fieldName}${r" AND ''!= "}${fieldItem.fieldName}${r'">'}
                        ${fieldItem.columnName} = ${r"#{"}${fieldItem.fieldName}${r"}"},
                        ${r"</if>"}
                    </#if>
                </#if>
            </#list>
        </set>
        WHERE id = ${r"#{"}id${r"}"}
    </update>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM ${classInfo.tableName}
        WHERE id = ${r"#{id}"}
    </select>

    <select id="selectListByCondition" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM ${classInfo.tableName}
        <where>
            <#list classInfo.fieldList as fieldItem >
                <#if fieldItem.fieldClass == "LocalDateTime" || fieldItem.fieldClass == "Date">
                    <#if fieldItem.columnName != "id" >
                        ${r'<if test ="null != '}${fieldItem.fieldName}${r'">'}
                            AND ${fieldItem.columnName} = ${r"#{"}${fieldItem.fieldName}${r"}"}
                        ${r"</if>"}
                    </#if>
                </#if>
                <#if fieldItem.fieldClass != "LocalDateTime" >
                    <#if fieldItem.columnName != "id" >
                        ${r'<if test ="null != '}${fieldItem.fieldName}${r" AND ''!= "}${fieldItem.fieldName}${r'">'}
                            AND ${fieldItem.columnName} = ${r"#{"}${fieldItem.fieldName}${r"}"}
                        ${r"</if>"}
                    </#if>
                </#if>
            </#list>
        </where>
    </select>

</mapper>